package com.eshop.modules.user.service.impl;

import java.lang.invoke.SerializedLambda;
import java.io.IOException;
import com.eshop.utils.FileUtil;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import com.eshop.common.utils.QueryHelpPlus;
import java.util.LinkedHashMap;
import java.io.Serializable;
import com.eshop.modules.shop.domain.SystemUserLevel;
import com.eshop.modules.user.service.dto.SystemUserTaskDto;
import com.github.pagehelper.PageInfo;
import java.util.Map;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.user.service.dto.SystemUserTaskQueryCriteria;
import java.util.Iterator;
import java.text.MessageFormat;
import cn.hutool.core.util.NumberUtil;
import com.eshop.modules.user.vo.SystemUserTaskQueryVo;
import com.eshop.modules.user.service.dto.TaskDto;
import java.util.Collection;
import com.eshop.modules.user.domain.UserTaskFinish;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.function.Function;
import java.util.List;
import com.eshop.enums.ShopCommonEnum;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.eshop.modules.user.service.UserBillService;
import com.eshop.modules.user.service.UserTaskFinishService;
import com.eshop.modules.order.service.mapper.StoreOrderMapper;
import com.eshop.modules.user.service.mapper.UserBillMapper;
import com.eshop.modules.user.service.mapper.UserTaskFinishMapper;
import com.eshop.modules.user.service.SystemUserLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.user.service.SystemUserTaskService;
import com.eshop.modules.user.domain.SystemUserTask;
import com.eshop.modules.user.service.mapper.SystemUserTaskMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class SystemUserTaskServiceImpl extends BaseServiceImpl<SystemUserTaskMapper, SystemUserTask> implements SystemUserTaskService
{
    @Autowired
    private IGenerator generator;
    @Autowired
    private SystemUserLevelService systemUserLevelService;
    @Autowired
    private SystemUserTaskMapper yxSystemUserTaskMapper;
    @Autowired
    private UserTaskFinishMapper userTaskFinishMapper;
    @Autowired
    private UserBillMapper userBillMapper;
    @Autowired
    private StoreOrderMapper storeOrderMapper;
    @Autowired
    private UserTaskFinishService userTaskFinishService;
    @Autowired
    private UserBillService userBillService;
    
    public int getTaskComplete(final int levelId, final Long uid) {
        final List<SystemUserTask> list = (List<SystemUserTask>)((LambdaQueryChainWrapper)(this.lambdaQuery().eq(SystemUserTask::getLevelId, levelId)).eq(SystemUserTask::getIsShow, ShopCommonEnum.SHOW_1.getValue())).list();
        final List<Integer> taskIds = list.stream().map(SystemUserTask::getId).collect(Collectors.toList());
        if (taskIds.isEmpty()) {
            return 0;
        }
        final int count = this.userTaskFinishMapper.selectCount((Wrapper)(Wrappers.<UserTaskFinish>lambdaQuery().in(UserTaskFinish::getTaskId, taskIds)).eq(UserTaskFinish::getUid, uid));
        return count;
    }
    
    public TaskDto getTaskList(final int levelId, final Long uid) {
        final LambdaQueryWrapper<SystemUserTask> wrapper = (LambdaQueryWrapper<SystemUserTask>)new LambdaQueryWrapper();
        ((wrapper.eq(SystemUserTask::getLevelId, levelId)).eq(SystemUserTask::getIsShow, ShopCommonEnum.SHOW_1.getValue())).orderByDesc(SystemUserTask::getSort);
        final List<SystemUserTaskQueryVo> list = (List<SystemUserTaskQueryVo>)this.generator.convert(this.yxSystemUserTaskMapper.selectList((Wrapper)wrapper), (Class)SystemUserTaskQueryVo.class);
        final TaskDto taskDTO = new TaskDto();
        taskDTO.setList(list);
        taskDTO.setReachCount(this.getTaskComplete(levelId, uid));
        taskDTO.setTask(this.tidyTask(list, uid));
        return taskDTO;
    }

    private List<SystemUserTaskQueryVo> tidyTask(List<SystemUserTaskQueryVo> task, Long uid) {
        Iterator var3 = task.iterator();

        while(var3.hasNext()) {
            SystemUserTaskQueryVo taskQueryVo = (SystemUserTaskQueryVo)var3.next();
            int count = ((this.userTaskFinishService.lambdaQuery().eq(UserTaskFinish::getTaskId, taskQueryVo.getId())).eq(UserTaskFinish::getUid, uid)).count();
            if (count > 0) {
                taskQueryVo.setNewNumber(taskQueryVo.getNumber());
                taskQueryVo.setSpeed(100);
                taskQueryVo.setFinish(ShopCommonEnum.IS_FINISH_1.getValue());
                taskQueryVo.setTaskTypeTitle("");
            } else {
                double sumNumber = 0.0D;
                String title = "";
                String var9 = taskQueryVo.getTaskType();
                byte var10 = -1;
                switch(var9.hashCode()) {
                    case -1041635236:
                        if (var9.equals("CumulativeAttendance")) {
                            var10 = 2;
                        }
                        break;
                    case 641273779:
                        if (var9.equals("ConsumptionAmount")) {
                            var10 = 1;
                        }
                        break;
                    case 2010591864:
                        if (var9.equals("SatisfactionIntegral")) {
                            var10 = 0;
                        }
                }

                switch(var10) {
                    case 0:
                        sumNumber = this.userBillMapper.sumIntegral(uid);
                        title = "还需要{0}经验";
                        break;
                    case 1:
                        sumNumber = this.storeOrderMapper.sumPrice(uid);
                        title = "还需消费{0}元";
                        break;
                    case 2:
                        sumNumber = (double)this.userBillService.cumulativeAttendance(uid);
                        title = "还需签到{0}天";
                }

                if (sumNumber >= (double)taskQueryVo.getNumber()) {
                    this.userTaskFinishService.setFinish(uid, taskQueryVo.getId());
                    taskQueryVo.setFinish(ShopCommonEnum.IS_FINISH_1.getValue());
                    taskQueryVo.setSpeed(100);
                    taskQueryVo.setTaskTypeTitle("");
                    taskQueryVo.setNewNumber(taskQueryVo.getNumber());
                } else {
                    double numdata = NumberUtil.sub(taskQueryVo.getNumber().doubleValue(), sumNumber);
                    taskQueryVo.setTaskTypeTitle(MessageFormat.format(title, numdata));
                    double speed = NumberUtil.div(sumNumber, taskQueryVo.getNumber().doubleValue());
                    taskQueryVo.setSpeed(Double.valueOf(NumberUtil.mul(speed, 100.0F)).intValue());
                    taskQueryVo.setFinish(ShopCommonEnum.IS_FINISH_0.getValue());
                    taskQueryVo.setNewNumber(Double.valueOf(sumNumber).intValue());
                }
            }
        }

        return task;
    }
    
    public Map<String, Object> queryAll(final SystemUserTaskQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<SystemUserTask> page = (PageInfo<SystemUserTask>)new PageInfo((List)this.queryAll(criteria));
        final List<SystemUserTaskDto> systemUserTaskDTOS = (List<SystemUserTaskDto>)this.generator.convert(page.getList(), (Class)SystemUserTaskDto.class);
        for (final SystemUserTaskDto systemUserTaskDTO : systemUserTaskDTOS) {
            final SystemUserLevel userLevel = (SystemUserLevel)this.systemUserLevelService.getById((Serializable)systemUserTaskDTO.getLevelId());
            if (userLevel == null) {
                continue;
            }
            systemUserTaskDTO.setLevalName(userLevel.getName());
        }
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", systemUserTaskDTOS);
        map.put("totalElements", page.getTotal());
        return map;
    }
    
    public List<SystemUserTask> queryAll(final SystemUserTaskQueryCriteria criteria) {
        return (List<SystemUserTask>)((SystemUserTaskMapper)this.baseMapper).selectList((Wrapper)QueryHelpPlus.getPredicate(SystemUserTask.class, criteria));
    }
    
    public void download(final List<SystemUserTaskDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final SystemUserTaskDto yxSystemUserTask : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("任务名称", yxSystemUserTask.getName());
            map.put("配置原名", yxSystemUserTask.getRealName());
            map.put("任务类型", yxSystemUserTask.getTaskType());
            map.put("限定数", yxSystemUserTask.getNumber());
            map.put("等级id", yxSystemUserTask.getLevelId());
            map.put("排序", yxSystemUserTask.getSort());
            map.put("是否显示", yxSystemUserTask.getIsShow());
            map.put("是否务必达成任务,1务必达成,0=满足其一", yxSystemUserTask.getIsMust());
            map.put("任务说明", yxSystemUserTask.getIllustrate());
            map.put("新增时间", yxSystemUserTask.getAddTime());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
}
